package com.lehecai.core.lottery.content.util;

import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.content.impl.LotteryTypeSpecifiedElementAttribute;

import java.util.*;

/**
 * User: sunshow
 */
public class LotteryTypeSpecifiedElementResult {

    private Map<String, List<LotteryTypeSpecifiedElementAttribute>> elementMap;

    private Map<String, List<LotteryType>> elementLotteryTypeMap = new HashMap<String, List<LotteryType>>();

    private boolean single = true;

    protected LotteryTypeSpecifiedElementResult() {

    }

    public LotteryTypeSpecifiedElementResult(Map<String, List<LotteryTypeSpecifiedElementAttribute>> elementMap) {
        super();
        this.elementMap = Collections.unmodifiableMap(elementMap);

        for (String element : elementMap.keySet()) {
            if (!elementLotteryTypeMap.containsKey(element)) {
                elementLotteryTypeMap.put(element, new ArrayList<LotteryType>());
            }
            List<LotteryType> lotteryTypeList = elementLotteryTypeMap.get(element);

            List<LotteryTypeSpecifiedElementAttribute> attributeList = this.elementMap.get(element);
            if (attributeList != null) {
                for (LotteryTypeSpecifiedElementAttribute elementAttribute : attributeList) {
                    if (!lotteryTypeList.contains(elementAttribute.getLotteryType())) {
                        lotteryTypeList.add(elementAttribute.getLotteryType());
                    }
                }
            }
        }

        LotteryType lotteryType = null;
        for (List<LotteryType> lotteryTypeList : elementLotteryTypeMap.values()) {
            if (lotteryTypeList.isEmpty()) {
                continue;
            }
            if (lotteryTypeList.size() > 1) {
                this.single = false;
                break;
            }
            if (lotteryType == null) {
                lotteryType = lotteryTypeList.iterator().next();
                continue;
            }

            if (!lotteryTypeList.contains(lotteryType)) {
                this.single = false;
                break;
            }
        }
    }

    public List<LotteryTypeSpecifiedElementAttribute> get(String element, LotteryType lotteryType) {
        if (!elementMap.containsKey(element)) {
            return null;
        }

        List<LotteryTypeSpecifiedElementAttribute> elementAttributeList = new ArrayList<LotteryTypeSpecifiedElementAttribute>();

        for (LotteryTypeSpecifiedElementAttribute elementAttribute : elementMap.get(element)) {
            if (elementAttribute.getLotteryType().getValue() == lotteryType.getValue()) {
                elementAttributeList.add(elementAttribute);
            }
        }

        if (elementAttributeList.isEmpty()) {
            return null;
        }

        return elementAttributeList;
    }

    public String[] getAttribute(String element, LotteryType lotteryType) {
        List<LotteryTypeSpecifiedElementAttribute> attributeList = this.get(element, lotteryType);

        if (attributeList == null || attributeList.isEmpty()) {
            return null;
        }

        String[] attributes = new String[attributeList.size()];

        for (int i = 0; i < attributeList.size(); i++) {
            attributes[i] = attributeList.get(i).getAttribute();
        }

        return attributes;
    }
    
    public String[] getAttributeString(String element, LotteryType lotteryType) {
        List<LotteryTypeSpecifiedElementAttribute> attributeList = this.get(element, lotteryType);

        if (attributeList == null || attributeList.isEmpty()) {
            return null;
        }

        String[] attributes = new String[attributeList.size()];

        for (int i = 0; i < attributeList.size(); i++) {
            attributes[i] = attributeList.get(i).toString();
        }

        return attributes;
    }    

    /**
     * @return 是否所有element中只包含一个彩种的投注
     */
    public boolean isSingle() {
        return this.single;
    }

    /**
     * 如果是单一彩种，返回该彩种，否则返回null
     * @return
     */
    public LotteryType getSingleLotteryType() {
        if (this.isSingle()) {
            return this.elementLotteryTypeMap.values().iterator().next().iterator().next();
        }
        return null;
    }

    public int getElementLotteryCount(String element) {
        List<LotteryType> lotteryTypeList = this.getElementLotteryType(element);
        if (lotteryTypeList == null) {
            return  0;
        }
        return lotteryTypeList.size();
    }

    public List<LotteryType> getElementLotteryType(String element) {
        if (this.elementLotteryTypeMap.containsKey(element)) {
            return this.elementLotteryTypeMap.get(element);
        }
        return null;
    }
}
